//
//  AdcTabar.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/7/3.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

@objc protocol TCAudienceTabarDelegate {
    func adcTabar_sendBarrge(text:String)
    func adcTabar_hasBarrage(adcTabar:TCAudienceTabar,senber:ZHButton)
}
class TCAudienceTabar: NSView {
    
    var delegate:TCAudienceTabarDelegate?
    var isHandUp:Bool = false {
        didSet {
            let image = isHandUp ? "icon_adc_hand":"icon_adc_nohand"
            self.handUpBtn.iconImage = NSImage.image(name:image)
        }
    }
    
    func readHasHands() {
        let count = Td.hands.count
        if count > 0 {
            self.messageBtn.isHidden = false
            self.messageBtn.zh_title = "\(count)"+" 人举手"
        }else {
            self.messageBtn.isHidden = true
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        self.addSubview(niceBtn)
        niceBtn.snp.makeConstraints { (make) in
            make.height.width.equalTo(40)
            make.left.equalTo(20)
            make.bottom.equalTo(-30)
        }
        
        
        self.addSubview(barrgeHiddenBtn)
        barrgeHiddenBtn.snp.makeConstraints { (make) in
            make.height.width.equalTo(40)
            make.left.equalTo(niceBtn.snp.right).offset(20)
            make.bottom.equalTo(-30)
        }
        
        self.addSubview(barrgeView)
        barrgeView.snp.makeConstraints { (make) in
            make.width.equalTo(260)
            make.height.equalTo(40)
            make.left.equalTo(barrgeHiddenBtn.snp.right).offset(20)
            make.bottom.equalTo(-30)
        }
        
        
        self.addSubview(handUpBtn)
        handUpBtn.snp.makeConstraints { (make) in
            make.height.width.equalTo(60)
            make.right.equalTo(-20)
            make.bottom.equalTo(-30)
        }
        
        
        self.addSubview(upMicBtn)
        upMicBtn.snp.makeConstraints { (make) in
            make.height.width.equalTo(40)
            make.right.equalTo(handUpBtn.snp.left).offset(-20)
            make.bottom.equalTo(-30)
        }
        
        self.addSubview(messageBtn)
        messageBtn.snp.makeConstraints { (make) in
            make.height.equalTo(40)
            make.width.equalTo(120)
            make.right.equalTo(upMicBtn.snp.left).offset(-20)
            make.bottom.equalTo(-30)
        }
    }
    
    lazy var niceBtn: ZHButton = {
        let btn = ZHButton()
        btn.normalImage = NSImage.image(name:"icon_setting_list")!
        btn.hasBorder = true
        btn.zh_cornerRadius = 20
        btn.zh_backgroundColor = color_dark
        btn.target = self
        btn.action = #selector(niceBtnAction(senber:))
        return btn
    }()
    
    
    lazy var barrgeHiddenBtn: ZHButton = {
        let btn = ZHButton()
        btn.iconImage = NSImage.image(name:"icon_barrge_dis")
        btn.hasBorder = true
        btn.zh_cornerRadius = 20
        btn.zh_backgroundColor = color_dark
        btn.target = self
        btn.action = #selector(hasBarrgeBtnAction(senber:))
        return btn
    }()
    
    lazy var barrgeView: TCAudienceBarrgeView = {
        let view = TCAudienceBarrgeView.init { (text) in
            if self.delegate != nil {
                self.delegate!.adcTabar_sendBarrge(text:text)
            }
        }
        view.wantsLayer = true
        view.layer?.cornerRadius = 20
        view.layer?.masksToBounds = true
        view.layer?.backgroundColor = color_dark.cgColor
        return view
    }()
    
    lazy var messageBtn: ZHButton = {
        let btn = ZHButton()
        btn.zh_title = "0 人举手"
        btn.zh_font = NSFont.systemFont(ofSize: 20)
        btn.zh_color = color_gray(255)
        btn.hasBorder = true
        btn.zh_cornerRadius = 20
        btn.zh_backgroundColor = color_main140
        btn.isHidden = true
        btn.target = self
        btn.action = #selector(messageBtnAction(senber:))
        return btn
    }()
    
    lazy var upMicBtn: ZHButton = {
        let btn = ZHButton()
        btn.normalImage = NSImage.image(name:"icon_setting_upmic")!
        btn.hasBorder = true
        btn.zh_cornerRadius = 20
        btn.zh_backgroundColor = color_dark
        btn.target = self
        btn.action = #selector(upMicBtnAction(senber:))
        return btn
    }()
    
    lazy var handUpBtn: ZHButton = {
        let btn = ZHButton()
        btn.iconImage = NSImage.image(name:"icon_adc_nohand")
        btn.hasBorder = true
        //btn.zh_cornerRadius = 20
        //btn.zh_backgroundColor = color_dark
        btn.target = self
        btn.action = #selector(handUpBtnAction(senber:))
        return btn
    }()
    
}

extension TCAudienceTabar {
    @objc func handUpBtnAction(senber:ZHButton) {
        self.isHandUp = !self.isHandUp
        MTNot.postNotHandUp(isok: self.isHandUp)
        IMcmd.shared.handUp(isok: self.isHandUp)
    }
    
    @objc func hasBarrgeBtnAction(senber:ZHButton) {
        if self.delegate != nil {
            self.delegate?.adcTabar_hasBarrage(adcTabar: self, senber: senber)
        }
    }
    
    @objc func niceBtnAction(senber:ZHButton) {
        let controller = TCAudienceUSEREngViewController.init(isStand: true)
        let window = NSWindow.init(contentViewController: controller)
        window.orderInFont(onWindow: self.window, sender: nil)
        window.title = "参会列表"
    }
    
    @objc func messageBtnAction(senber:ZHButton) {
        let controller = TCAudienceUSEREngViewController.init(isStand: true,selectInx: 1)
        let window = NSWindow.init(contentViewController: controller)
        window.orderInFont(onWindow: self.window, sender: nil)
        window.title = "参会列表"
        /*
        let controller = MessageHandViewController()
        let popover = NSPopover.create(controller: controller)
        popover.show(senber, preferredEdge: NSRectEdge.maxY)
        */
    }
    
    @objc func upMicBtnAction(senber:ZHButton) {
        if  Td.sh().cloud.disLink == false {
            if TCGirdRender.shared().canAdd() {
                if let viewController = self.jk_viewcontroller() {
                    let asV = KlViewController.init(title: "申请请求上麦", massage: "立即向主持人发送上麦的请求？", buttomTitles: ["确认","取消"], indexBlock: { (index, controller) in
                        if index == 0 {
                            IMcmd.shared.upMic(isok: true)
                        }
                    }, navBlock: nil)
                    let window = NSWindow.init(contentViewController: asV)
                    window.makeKeyAndOrderFront(nil)
                }
            }else {
                show_msg("上麦人数已达上限")
            }
        }else {
            show_msg("主持人已禁止上麦")
        }
    }
}
